import { QuestionCircleOutlined } from '@ant-design/icons'
import { message, Space, Tooltip } from 'antd'
import React from 'react'
import style from './Index.module.less'
import { getDetailRequest } from './ajax'
import { awaitWrap, formatDate } from '@src/assets/js/tool'

async function initDetail (id, callback) {
  const h = message.loading('加载中', 0)
  const [e, d] = await awaitWrap(getDetailRequest(id))
  h()
  if (e === null && d !== null) {
    callback(d[0])
  }
}

// 获取表格字段配置
export function getColumns () {
  return [
    {
      title: '序号',
      dataIndex: 'key',
      key: 'index',
      align: 'center',
      width: 60
    },
    {
      title: '类型',
      dataIndex: 'type',
      key: 'type',
      width: 100
    },
    {
      title: '状态',
      dataIndex: 'status',
      key: 'status',
      width: 100,
      render: (v, line) => {
        if (v === '审核不通过') {
          return (
            <Space size={4}>
              <span>{v}</span>
              {line.audit_remark && (
                <Tooltip trigger={['click']} title={line.audit_remark}>
                  <QuestionCircleOutlined className={style.remark} />
                </Tooltip>
              )}
            </Space>
          )
        }
        return v
      }
    },
    {
      title: '申请时间',
      dataIndex: 'apply_datetime',
      key: 'apply_datetime',
      width: 100,
      render: (v) => v && formatDate(v)
    },
    {
      title: '审批时间',
      dataIndex: 'audit_datetime',
      key: 'audit_datetime',
      width: 100,
      render: (v) => v && formatDate(v)
    },
    {
      title: '操作',
      dataIndex: 'id',
      key: 'id',
      width: 100,
      render: (v, line) => {
        return (
          <Space>
            {line.type === '申请' && (
              <a onClick={() => {
                initDetail(line.institutionid, (d) => {
                  this.setState({ detailVisible: true, modifyData: d })
                })
              }}
              >分支机构详情
              </a>
            )}
            {line.status === '待审核' && <a onClick={() => { this.setState({ visible: true, modifyData: JSON.parse(JSON.stringify(line)) }) }}>审核</a>}
            {line.status !== '待审核' && <a onClick={() => { this.setState({ auditDetailVisible: true, modifyData: JSON.parse(JSON.stringify(line)) }) }}>详情</a>}
          </Space>
        )
      }
    }
  ]
}
